package Array;

public class lc977 {
    public int[] sortedSquares(int[] nums) {
        int left = 0;
        int right = nums.length;
        int[] res = new int[nums.length];
        int re = nums.length - 1;
        while (left < right) {
            long l = nums[left] * nums[left];
            long r = nums[right - 1] * nums[right - 1];
            if (l >= r) {
                res[re] = (int)l;
                left++;
            } else {
                res[re] = (int)r;
                right--;
            }
            re--;
        }
        return res;
    }
}